# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models


# 库中的模型
class Book(models.Model):
    id = models.AutoField(primary_key=True)  # id 会自动创建,可以手动写入
    title = models.CharField(max_length=32)  # 书籍名称
    price = models.DecimalField(max_digits=5, decimal_places=2)  # 书籍价格
    publish = models.CharField(max_length=32)  # 出版社名称
    pub_date = models.DateField()  # 出版时间

    def __str__(self):
        return "%d:%s:%d:%s:%s" % (self.id, self.title, self.price, self.publish, self.pub_date)

    # 自定义对应的表名
    class Meta:
        db_table = "app_book"
        verbose_name = '书籍信息'
        verbose_name_plural = '书籍信息管理'


# 书籍表 Book：title 、 price 、 pub_date 、 publish（外键，多对一） 、 authors（多对多）
class BookData(models.Model):
    id = models.AutoField(primary_key=True)  # id 会自动创建,可以手动写入
    title = models.CharField(max_length=32)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    pub_date = models.DateField()
    # 外键 外键在一对多的多中设置：models.ForeignKey("关联类名", on_delete=models.CASCADE)。
    # Django1.1 版本不需要联级删除：on_delete=models.CASCADE，Django2.2 需要。
    publish = models.ForeignKey("Publish", on_delete=models.CASCADE)
    # 多对多
    authors = models.ManyToManyField("Author")

    def __str__(self):
        return "%d:%s:%d:%s:%s" % (self.id, self.title, self.price, self.publish, self.pub_date)

    # 自定义对应的表名
    class Meta:
        db_table = "pc_book_data"
        verbose_name = '书籍信息'
        verbose_name_plural = '书籍信息管理'


# 出版社表 Publish：name 、 city 、 email
class Publish(models.Model):
    name = models.CharField(max_length=32)
    city = models.CharField(max_length=64)
    email = models.EmailField()

    def __str__(self):
        return "%d:%s:%s:%s" % (self.id, self.name, self.city, self.email)

    class Meta:
        db_table = "pc_publish"


# 作者表 Author：name 、 age 、 au_detail（一对一）
class Author(models.Model):
    name = models.CharField(max_length=32)
    age = models.SmallIntegerField()
    # 一对一 OneToOneField = ForeignKey(...，unique=True)设置一对一。
    au_detail = models.OneToOneField("AuthorDetail", on_delete=models.CASCADE)

    def __str__(self):
        return "%d:%s:%s:%s" % (self.id, self.name, self.age, self.au_detail)

    class Meta:
        db_table = "pc_author"


# 作者详情表 AuthorDetail：gender 、 tel 、 addr 、 birthday
class AuthorDetail(models.Model):
    gender_choices = (
        (0, "女"),
        (1, "男"),
        (2, "保密"),
    )
    gender = models.SmallIntegerField(choices=gender_choices)
    tel = models.CharField(max_length=32)
    addr = models.CharField(max_length=64)
    birthday = models.DateField()

    def __str__(self):
        return "%d:%s:%s:%s:%s" % (self.id, self.gender, self.tel, self.addr, self.birthday)

    class Meta:
        db_table = "pc_authorDetail"


# Create your models here.
class ArticleUpload(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    pic = models.ImageField(upload_to='cars/')  # 必须新建一个cars文件夹
